<?php

class Hung_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract {

    /**
     * Check permission user
     * @author Nguyen Manh Hung
     * @email manhhung86it@gmail.com
     * @version 1.0
     * @param Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        $module = $request->getParam('module', 'front');
        $auth = Zend_Auth::getInstance();
        $controller = $request->getControllerName();
        $action = $request->getActionName();
        $redirector = new Zend_Controller_Action_Helper_Redirector();
        //check quyen truy cap vao admin
        if ($module == 'admin') {
            if (!$auth->hasIdentity()) {
                //chua dang nhap
                $redirector->gotoUrlAndExit(SITE_URL . '/user/signin.html');
            }
        }
        //het check quyen truy cap vao admin
        // check quyen truy nhap vao module user
        if ($module == 'user') {
            if (!$auth->hasIdentity()) {
                if ($controller == 'index') {
                    if (($action == 'login' || $action == 'register' || $action == 'resetpass' || $action == 'signin' || $action == 'fpass' || $action == 'signup' || $action == 'ranking')) {
                        //cho no muon lam gi thi lam
                    } else {
                        $redirector->gotoUrlAndExit(SITE_URL . '/user/signin.html');
                    }
                } else {
                    $redirector->gotoUrlAndExit(SITE_URL . '/user/my-info.html');
                }
            }
        }
        //het check quyen truy cap vao user
    }

}

